package org.jetnuke.modules.story_archive;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;

public class ShowMonth implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ShowMonth.class);

	String month;

	String year;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		int year_ = Integer.parseInt(year);
		if (month.startsWith("0"))
			month = month.substring(1);
		int month_ = Integer.parseInt(month);
		Calendar c = new GregorianCalendar(year_, month_ - 1, 1);
		Calendar c1 = new GregorianCalendar(year_, month_ - 1, 1);
		c1.add(Calendar.MONTH, 1);

		/*
		 * Criteria crt = new Criteria(); Criteria.Criterion a1 =
		 * crt.getNewCriterion(storiesPeer.TIME, c .getTime(),
		 * Criteria.GREATER_EQUAL); Criteria.Criterion a2 =
		 * crt.getNewCriterion(storiesPeer.TIME, c1 .getTime(),
		 * Criteria.LESS_THAN); crt.add(a1.and(a2));
		 * 
		 * List ls = storiesPeer.doSelect(crt
		 * .addDescendingOrderByColumn(storiesPeer.SID));
		 */
		List ls = DataAccessDriver.getInst().newStoriesDAO().getStoriesInTime(
				c.getTime(), c1.getTime());
		vc.put("stories", ls);
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public void setYear(String year) {
		this.year = year;
	}
}
